// 通过.evm文件设置环境变量
const dotenv = require("dotenv");
const {NODE_ENV} = process.env;
// 设置环境变量
dotenv.config({
    path: `${__dirname}/env/${NODE_ENV}.env`
});

const swaggerJSDoc = require('swagger-jsdoc');
const swaggerUi = require('swagger-ui-express');
const express = require('express');
const cookieParser = require('cookie-parser');
const logger = require('morgan');

const router = require("./routes/index");
const {noRouter} = require("./middleware");
const swaggerOptions = require("./swaggerDocument");
// 初始化
const app = express();

// 开发环境打印日志
if (process.env.NODE_ENV) {
    app.use(logger('dev'));
}
// 将请求体解析为json
app.use(express.json());
// 解析表单中的请求体数据，以json数据返回
app.use(express.urlencoded({extended: true}));

// 配置cookie中间件
app.use(cookieParser());

// api中间件
app.use('/api', router);
const swaggerSpec = swaggerJSDoc(swaggerOptions);
// cs接口文档
app.use("/api-docs", swaggerUi.serve, swaggerUi.setup(swaggerSpec))

// catch 404 and forward to error handler
app.use(noRouter);

module.exports = app;
